package com.example.servlets;

import jakarta.servlet.annotation.WebServlet;
import org.json.JSONArray;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/CheckoutServlet")
public class CheckoutServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        JSONObject jsonResponse = new JSONObject();

        try {
            // 获取请求中的数据
            String jsonString = request.getReader().lines().reduce("", (accumulator, actual) -> accumulator + actual);
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONArray items = jsonObject.getJSONArray("items");

            // 数据库连接
            String dbURL = "jdbc:mysql://localhost:3306/game";
            String dbUser = "root";
            String dbPassword = "1234";
            Connection connection = DriverManager.getConnection(dbURL, dbUser, dbPassword);

            // 假设这些值是从请求或其他来源获取的
            BigDecimal total = new BigDecimal("100.00"); // 示例总金额
            BigDecimal amount = new BigDecimal("100.00"); // 示例支付金额
            String status = "Pending"; // 示例订单状态
            String paytype = "Credit Card"; // 示例支付方式
            String gamename = "Example Game"; // 示例游戏名
            String gameimg = "example.jpg"; // 示例游戏照片
            String gametxt = "Example game description"; // 示例游戏简介

            // 插入数据
            String sql = "INSERT INTO orders (user_id, item_details, total, amount, status, paytype, gamename, gameimg, gametxt) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setInt(1, (Integer) request.getSession().getAttribute("user_id")); // 假设用户ID存储在session中
            statement.setString(2, items.toString());
            statement.setBigDecimal(3, total);
            statement.setBigDecimal(4, amount);
            statement.setString(5, status);
            statement.setString(6, paytype);
            statement.setString(7, gamename);
            statement.setString(8, gameimg);
            statement.setString(9, gametxt);
            statement.executeUpdate();

            // 返回成功响应
            jsonResponse.put("success", true);
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResponse.put("success", false);
            jsonResponse.put("error", "数据库错误：" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            jsonResponse.put("success", false);
            jsonResponse.put("error", "系统错误：" + e.getMessage());
        }

        out.print(jsonResponse.toString());
        out.flush();
    }
} 